package br.com.velp.sudecap.util;

import br.com.velp.sudecap.entidade.Usuario;
import br.com.velp.sudecap.mb.contexto.ContextoBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * <sumario> 
 * <objetivo> "Rastreabilidade" </objetivo> 
 * <autor> EDUARDO SANTOS DE OLIVEIRA </autor> 
 * <versao>1.0.0.0</versao> 
 * <data_criacao> 17.10.2012 </data_criacao> 
 * <data_ultima_alteracao> 17.07.2013 </data_ultima_alteracao>
 * <descricao> Classe Utilitária Para Controle de 
 *             Operações Inerentes ao JSF
 * </descricao> 
 * </sumario>
 */
public class JsfUtil {
    
    public static Usuario getUsuarioLogado(){
        ContextoBean contextoBean = ((ContextoBean) recuperarObjetoSessao("contextoBean"));
        return contextoBean != null ? contextoBean.getUsuario() : null;
    }

    public static Object recuperarObjetoSessao(String key) {
        return getSession() != null ? getSession().getAttribute(key) : null;
    }
    
    public static Object recuperarObjetoAplicacao(String key){
        return ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getAttribute(key);
    }

    public static void adicionarObjetoSessao(String key, Object value) {
        getSession().setAttribute(key, value);
    }

    public static void adicionarObjetoAplicacao(String key, Object value){
        ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).setAttribute(key, value);
    }
    
    public static void removerObjetoSessao(String key) {
        getSession().removeAttribute(key);
    }
    
    public static void removerObjetoAplicacao(String key){
        ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).removeAttribute(key);
    }

    public static boolean sessaoContemObjeto(String key) {
        return recuperarObjetoSessao(key) != null;
    }
    
    public static boolean aplicacaoContemObjeto(String key){
        return recuperarObjetoAplicacao(key) != null;
    }

    public static Object getSpringBean(String controllerName) {
        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        return context.getBean(controllerName);
    }
    
    public static String getRealPath(String input){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ServletContext servletContext = (ServletContext)facesContext.getExternalContext().getContext();  
        return servletContext.getRealPath(input); 
    }
    
    public static String getContexto() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest origRequest = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        return origRequest.getContextPath();
    }
    
    public static HttpSession getSession() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (context == null){
            return null;
        }
        ExternalContext externalContext = context.getExternalContext();
        HttpSession session = (HttpSession) externalContext.getSession(true);
        return session;
    }
    
}
